﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;

public partial class DynamicSimple : ExtBaseRpt
{
    protected void Page_Load(object sender, EventArgs e)
    {
        mid = Request.Params["mid"];
        if (!X.IsAjaxRequest)
        {
            //加载表头 查找网格
            this.MainPanel.ColumnModel.Add(BuildColumns(mid, Session["UserID"].ToString(), "网格", EditPanel));
            BtnFind(sender, null);
        }
    }
    protected void MyData_Refresh(object sender, StoreReadDataEventArgs e)
    {
        BtnFind(sender, null);
    }
    protected void BtnFind(object sender, DirectEventArgs e)
    {
        StoreColumns(MainStore, mid, "网格");
        string sql = "";
        sql = mydb.DM_GetExecuteScalarObj("select weburl from sys_modules where parentid=" + mid + " and modulecode='网格'").ToString();
        MainStore.DataSource = mydb.DM_GetDateSet(sql);
        MainStore.DataBind();
    }
    [DirectMethod(Namespace = "CompanyX")]
    public void BtnDelete()
    {
        RowSelectionModel sm = this.MainPanel.GetSelectionModel() as RowSelectionModel;
        string id = sm.SelectedRecordID;
         string sql = "";
        string tablename = mydb.DM_GetExecuteScalarObj("select moduletab from sys_modules where parentid=" + mid + " and modulecode='网格'").ToString();
        string keyname = mydb.DM_GetExecuteScalarObj(@"SELECT FieldName FROM Sys_ModulesLayout WHERE MID IN(select MId from sys_modules where parentid=" + mid + @" and modulecode='网格')
                                                        AND isnull(IsKeyId,0) = 1").ToString();
        sql = "delete from " + tablename + " where " + keyname + " = "+id;
        if (mydb.DM_ExecuteNonQuery(sql) != -1)
        {
            ShowBox("删除成功！");
        }
        else
        {
            ShowBox("删除失败！");
        }
    }
    [DirectMethod(Namespace = "CompanyX")]
    public void BtnSave(string fields,string vals)
    {
        //保存数据
        //fields = fields.TrimEnd(',');
        //vals = vals.TrimEnd(',');
        RowSelectionModel sm = this.MainPanel.GetSelectionModel() as RowSelectionModel;
        string id = sm.SelectedRecordID;
        ShowBox("ID:" + id);
        string sql = "";
        string tablename = mydb.DM_GetExecuteScalarObj("select moduletab from sys_modules where parentid=" + mid + " and modulecode='网格'").ToString();
        string keyname = mydb.DM_GetExecuteScalarObj(@"SELECT FieldName FROM Sys_ModulesLayout WHERE MID IN(select MId from sys_modules where parentid=" + mid + @" and modulecode='网格')
                                                        AND isnull(IsKeyId,0) = 1").ToString();
        //新增
        if (this.ActState.Value.ToString() == "Add")
        {
            string[] flist = fields.Split(',');
            string[] vlist = vals.Split(',');
            string flstr="",valstr="",tempstr = "";
            for (int i = 0; i < flist.Length; i++)
            {
                flstr += flist[i]+",";
                if (vlist[i] == "" && flist[i].ToLower() != "creater"
                   && flist[i].ToLower() != "createdate")
                {
                    tempstr = "''";
                }
                else if (vlist[i].ToLower() == "true")
                {
                    tempstr = "1";
                }
                else if (vlist[i].ToLower() == "false")
                {
                    tempstr = "0";
                }
                else if (flist[i].ToLower() == "creater")
                {
                    tempstr = "'"+Session["UserCode"].ToString()+"'";
                }
                else if (flist[i].ToLower() == "createdate")
                {
                    tempstr = "getdate()";
                }
                else
                {
                    tempstr = "'"+vlist[i]+"'";
                }
                valstr += tempstr+",";
            }
            flstr = flstr.TrimEnd(',');
            valstr = valstr.TrimEnd(',');
            sql = "insert into " + tablename + "(" + flstr + ")values(" + valstr + ")";
        }
        else
        {
            //修改

            string[] flist = fields.Split(',');
            string[] vlist = vals.Split(',');
            string flstr = "", tempstr = "";
            for (int i = 0; i < flist.Length; i++)
            {
                if (vlist[i] == "" && flist[i].ToLower() != "creater"
                    && flist[i].ToLower() != "createdate")
                {
                    tempstr = "''";
                }
                else if (vlist[i].ToLower() == "true")
                {
                    tempstr = "1";
                }
                else if (vlist[i].ToLower() == "false")
                {
                    tempstr = "0";
                }
                else
                {
                    tempstr = "'" + vlist[i] + "'";
                }
                flstr += flist[i]+"="+ tempstr + ",";
            }
            flstr = flstr.TrimEnd(',');
            sql = "update " + tablename + " set " + flstr + " where " + keyname + " = '" + id + "'";
        }
        //X.Msg.Alert("Submit", sql).Show();
        //this.Panel3.Collapsed = true;
        if (mydb.DM_ExecuteNonQuery(sql) != -1)
        {
            ShowBox("保存成功！");
            this.ActState.SetValue("Edit");
        }
        else
        {
            ShowBox("数据保存失败！");
        }
        BtnFind(null, null);
    }
    protected void SaveData(object sender, DirectEventArgs e)
    {

    }
}